{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 聚合操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np \n",
    "L = np.random.random(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([0.3742589 , 0.07398355, 0.36581429, 0.27017897, 0.99033419,\n       0.14851171, 0.79074631, 0.82037682, 0.96714547, 0.83318914,\n       0.675687  , 0.92312257, 0.75021745, 0.38686529, 0.34133363,\n       0.23232207, 0.64124997, 0.82838636, 0.42626094, 0.10895941,\n       0.86214392, 0.35647934, 0.16684465, 0.48965528, 0.01111342,\n       0.26214836, 0.15960834, 0.89987458, 0.01151272, 0.64848185,\n       0.15404531, 0.9596508 , 0.65668533, 0.33556667, 0.10472405,\n       0.63529459, 0.05322761, 0.36790874, 0.77413258, 0.05567979,\n       0.68732123, 0.70314445, 0.27456931, 0.93455354, 0.7862958 ,\n       0.77617624, 0.91317977, 0.91929807, 0.71072398, 0.2372247 ,\n       0.39631471, 0.55475029, 0.95623982, 0.41032708, 0.79318427,\n       0.38163116, 0.09983797, 0.44666125, 0.60478694, 0.67117579,\n       0.77742789, 0.0162644 , 0.12092896, 0.80680185, 0.74418262,\n       0.30318307, 0.96714009, 0.81652903, 0.92727485, 0.8461485 ,\n       0.31959777, 0.13951124, 0.74023257, 0.95358402, 0.67326594,\n       0.36796206, 0.61518688, 0.80189632, 0.36913721, 0.8180473 ,\n       0.21085721, 0.50146842, 0.77436054, 0.23735204, 0.21064102,\n       0.34046776, 0.37451946, 0.8246769 , 0.06279429, 0.63112379,\n       0.82530032, 0.9240517 , 0.76316794, 0.86538024, 0.25779013,\n       0.32692189, 0.42453255, 0.63926631, 0.34747078, 0.9198993 ])"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "53.757463505684356"
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "sum(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "53.75746350568435"
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "np.sum(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "1.21 s ± 19.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n12.5 ms ± 334 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
    }
   ],
   "source": [
    "big_array = np.random.random(10000000)\n",
    "%timeit sum(big_array)\n",
    "%timeit np.sum(big_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "3.1321299509556866e-07"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "np.min(big_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.999999899328932"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "np.max(big_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.999999899328932"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "big_array.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "5000373.241610027"
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "big_array.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 0,  1,  2,  3],\n       [ 4,  5,  6,  7],\n       [ 8,  9, 10, 11],\n       [12, 13, 14, 15]])"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "X = np.arange(16).reshape(4,-1)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "120"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "np.sum(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 6, 22, 38, 54])"
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "np.sum(X, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([24, 28, 32, 36])"
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "np.sum(X, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0"
     },
     "metadata": {},
     "execution_count": 15
    }
   ],
   "source": [
    "np.prod(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "2004189184"
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "source": [
    "np.prod(X +1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "7.5"
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "source": [
    "np.mean(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "7.5"
     },
     "metadata": {},
     "execution_count": 18
    }
   ],
   "source": [
    "np.median(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "3.2"
     },
     "metadata": {},
     "execution_count": 20
    }
   ],
   "source": [
    "v = np.array([1 ,1, 2,2, 10])\n",
    "np.mean(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "2.0"
     },
     "metadata": {},
     "execution_count": 21
    }
   ],
   "source": [
    "np.median(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.5000977050323312"
     },
     "metadata": {},
     "execution_count": 22
    }
   ],
   "source": [
    "np.percentile(big_array, q=50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.5000977050323312"
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "source": [
    "np.median(big_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.999999899328932"
     },
     "metadata": {},
     "execution_count": 24
    }
   ],
   "source": [
    "np.percentile(big_array, q=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.999999899328932"
     },
     "metadata": {},
     "execution_count": 25
    }
   ],
   "source": [
    "np.max(big_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "3.1321299509556866e-07\n0.2500547338499457\n0.5000977050323312\n0.7499680197584703\n0.999999899328932\n"
    }
   ],
   "source": [
    "for percent in [0 ,25, 50 ,75, 100]:\n",
    "    print(np.percentile(big_array, q=percent))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.08331634899522167"
     },
     "metadata": {},
     "execution_count": 27
    }
   ],
   "source": [
    "np.var(big_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.2886457153591954"
     },
     "metadata": {},
     "execution_count": 28
    }
   ],
   "source": [
    "np.std(big_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.random.normal(0 ,1,size=1000000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "-0.0004674751782410452"
     },
     "metadata": {},
     "execution_count": 31
    }
   ],
   "source": [
    "np.mean(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "1.0001509598817275"
     },
     "metadata": {},
     "execution_count": 32
    }
   ],
   "source": [
    "np.std(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}